// components/confession_wall_entry/confession_wall_entry.js
const app = getApp()

Component({
    /**
     * 组件的属性列表
     */
    properties: {
        //数据
        entryData: Object,

        //在页面内的序号
        index: -1,

        //显示的宽度(rpx)
        width: Number
    },

    /**
     * 组件的初始数据
     */
    data: {
        hasImage: false, //有无图片
        height: 0, //图片显示的高度

        avatar: "",
    },

    /**
     * 组件的方法列表
     */
    methods: {
        /*点或取消赞*/
        switchLike: function () {
            if (app.globalData.userInfo == null) {
                wx.showToast({
                    title: '请先登录',
                    icon: 'error',
                    duration: 2000
                })
                return;
            }
            let that = this
            let entryData = this.data.entryData
            let index = this.data.index
            this.setData({
                entryData
            })
            var data = {
                textId: entryData.wallId,
                state: !entryData.wallliked
            }
            // 发送点赞或者取消点赞的请求
            app.wxapi('POST', "/wall/thumb/updateLikeState", data, 'application/x-www-form-urlencoded', res => {
                console.log(res);
                if (res.success) {
                    console.log("点赞成功");
                    entryData.wallliked = !entryData.wallliked
                    this.setData({
                        entryData
                    })
                    this.triggerEvent("switchLike", {entryIndex: index, liked: entryData.wallliked} )

                    if (entryData.wallliked) {
                        entryData.wallThumbs++;
                        this.setData({
                            entryData
                        })
                    } else {
                        entryData.wallThumbs--;
                        this.setData({
                            entryData
                        })
                    }
                } else {
                    console.log("没有回应");
                    wx.showModal({
                        title: '提示',
                        content: '提交失败',
                    });
                }
            }, (err) => {
                //调用服务端登录接口失败
                console.log(err.errMsg)
            })
        },

        /*展开*/
        unfold: function () {
            let entryData = this.properties.entryData
            var transportData = {}
            for(let key in entryData) {
                if (key != 'commentVoList')
                    transportData[key] = entryData[key]
            }

            wx.navigateTo({
              url: '../../pages/details/details?content=' + JSON.stringify(transportData)
            })
          }
    },

    lifetimes: {
        attached: function () {
            let entryData = this.properties.entryData
            let that = this
            console.log("表白墙组件创建")
            if (entryData.wallPicUrls != undefined && entryData.wallPicUrls.length != 0) {
                wx.getImageInfo({
                    src: entryData.wallPicUrls[0],
                    success: function (res) {
                        that.setData({
                            height: that.properties.width * res.height / res.width,
                            hasImage: true
                        })
                    },
                    fail: function () {
                        console.error("fail to load a image" + entryData.wallPicUrls[0])
                    }
                })
            }
        }

    }
})